package com.superhelper.common.mapper.stat;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.superhelper.common.domain.stat.StatDailyOrder;

public interface StatDailyOrderMapper {
    @Select("select shopId,`type` as shopType,count(1) as orderCount,"
            + "             count(if(`status`='invalid',1,null)) as invalidCount,"
            + "             ifnull(sum(if(`status`='invalid',income,null)),0) as invalidAmount,"
            + "             count(if(`status`='settled',1,null)) as settledCount,"
            + "             count(if(`status`='delivering',1,null)) as deliveringCount,"
            + "             ifnull(sum(oriPrice),0) as totalAmount,"
            + "             ifnull(sum(if(`status`  in('valid','settled','delivering'),income,null)),0) as realAmount,"
            + "             ifnull(sum(if(`status`  in('valid','settled','delivering'),shopPart,null)),0) as shopPartAmount,"
            + "             ifnull(sum(if(`status`  in('valid','settled','delivering'),plfPart,null)),0) as plfPartAmount"
            + " from takeout_order where createAt >= #{startDate} and createAt <#{endDate} group by shopId,`type`")
    List<StatDailyOrder> statDailyOrder(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

    @Select("select auserShopId," + "    ifnull(sum(if(`status` not in(0,5),deliverFee,null)),0) as deliverAmount, "
            + "  ifnull(sum(if(`status` not in(0,5),currentTip,null)),0) as tipAmount    "
            + "    from deliver_record where createAt >= #{startDate} and createAt <#{endDate} group by auserShopId")
    List<StatDailyOrder> statDailyDeliver(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

    int batchAdd(List<StatDailyOrder> stats);

    @Update("update stat_daily_order set newUserCount = #{newUserCount}, oldUserCount=#{oldUserCount} where statDate=#{statDate}"
            + " and  aUserShopId=#{aUserShopId}")
    int updateUserCount(StatDailyOrder stat);
}
